<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 9, 2013
 * Time: 7:53:07 PM
 * To change this template use File | Settings | File Templates.
 */

class DisController extends BaseController {
    function actionTest() {
        $pdf = new PDF();
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->SetFont('Times', '', 12);
        for ($i = 1; $i <= 10; $i++)
            $pdf->Cell(0, 10, 'Printing line number ' . $i, 0, 1);
        $pdf->SetFont('Times', '', 20);
        $pdf->Cell(0, 10, 'Printing line number ', 0, 1);
        $pdf->Output();
    }

    function actionCreate() {
        $distribution = new Distribution();
        $year = $distribution->getYear();
        $month = $distribution->getMonth();
        $institute = new Institute();
        $instituteList = $institute->getInstituteOptions();
        $this->render('create', array('year' => $year, 'month' => $month, 'institute' => $instituteList));

    }

    function actionAddMore() {
        $distribution = new Distribution();
        $year = $distribution->getYear();
        $month = $distribution->getMonth();
        $institute = new Institute();
        $instituteList = $institute->getInstituteOptions();
        $this->render('addMore', array('year' => $year, 'month' => $month, 'institute' => $instituteList));

    }

    function actionPrint() {
        $distribution = new Distribution();
        $year = $distribution->getYear();
        $month = $distribution->getMonth();
        $institute = new Institute();
        $instituteList = $institute->getInstituteOptions();
        $this->render('print', array('year' => $year, 'month' => $month, 'institute' => $instituteList));

    }

    function actionCollect() {
        $distribution = new Distribution();
        $year = $distribution->getYear();
        $month = $distribution->getMonth();
        $institute = new Institute();
        $instituteList = $institute->getInstituteOptions();
        $this->render('collect', array('year' => $year, 'month' => $month, 'institute' => $instituteList));

    }

    function actionManage() {
        $distribution = new Distribution();
        $year = $distribution->getYear();
        $month = $distribution->getMonth();
        $institute = new Institute();
        $instituteList = $institute->getInstituteOptions();
        $this->render('manage', array('year' => $year, 'month' => $month, 'institute' => $instituteList));

    }

    function actionCollectM() {
        $this->render('collectManually');
    }

    function actionGetEmpCardList() {
        $insId = $_POST['instituteId'];
        $secId = $_POST['sectionId'];
        $list = $_POST['list'];
        $empCard = new TldCard();
        $data = $empCard->getEmpTldCardListDistribution($insId, $secId, $list);
        echo json_encode($data);
    }

    function actionGetEmpCardListMore() {
        $insId = $_POST['instituteId'];
        $secId = $_POST['sectionId'];
        $list = $_POST['list'];
        $fromMonth = $_POST['fromMonth'];
        $fromYear = $_POST['fromYear'];
        $toMonth = $_POST['toMonth'];
        $toYear = $_POST['toYear'];
        $empCard = new TldCard();
        $data = $empCard->getEmpTldCardListDistributionMore($insId, $secId, $list, $fromMonth, $fromYear, $toMonth, $toYear);
        echo json_encode($data);
    }

    function actionGetCollection() {
        $insId = $_POST['institute_id'];
        $secId = $_POST['section_id'];
        $fromYear = $_POST['fyear'];
        $fromMonth = $_POST['fmonth'];
        $toYear = $_POST['tyear'];
        $toMonth = $_POST['tmonth'];
        $tldCard = new TldCard();
        $data = $tldCard->getDistributionCollection($insId, $secId, $fromYear, $fromMonth, $toYear, $toMonth);
        echo json_encode($data);
    }

    function actionCreateDistribution() {
        $id = $_POST['id'];
        $card = $_POST['card'];
        $instituteId = $_POST['institute'];
        $sectionId = $_POST['section'];
        $employId = $_POST['id'];
        $notReturn = $_POST['nr'];
        $remove = $_POST['rm'];
        $returnDate = $_POST['returnDate'];
        $year = $_POST['year'];
        $month = $_POST['month'];
        $tYear = $_POST['toYear'];
        $tMonth = $_POST['toMonth'];
        $bgCard = $_POST['bgCard'];
        $isEdit = $_POST['edit'];
        $listId = $_POST['listId'];
        $tldCard = new TldCard();
        $isNotValid = $tldCard->validateMonthYear($instituteId, $sectionId, $month, $year);
        if ($isEdit) {
            $isNotValid = false;
        }
        if (!$isNotValid) {
            $tldCard->setValidIssueTldCardArray();
            $validate = $tldCard->validateIssueCard($id, $card, $remove, $bgCard);

            if ($validate['valid']) {
                    $i = $tldCard->createDistribution($id, $instituteId, $sectionId, $employId, $card, $notReturn, $returnDate, $year, $month, $remove, $tMonth, $tYear, $bgCard);                if (is_bool($i)) {
                    if ($i) {
                        $_message = "Successsfuly Saved new Distribution ! ";
                        $_type = "success";
                    } else {
                        $_message = "Unsuccesssfuly Saved ! ";
                        $_type = "error";
                    }

                } else {
                    if ($i == -1) {
                        $_message = "No data was changed !";
                        $_type = "info";
                    } else if ($i == 1) {
                        $_message = "Successsfuly updated Section information ! ";
                        $_type = "success";
                    } else if ($i == 0) {
                        $_message = " Unsuccesssfuly update ! ";
                        $_type = "error";
                    } else {
                        $_message = $i . " is Not a valid TLD Card ";
                        $_type = "error";
                    }
                }
                $data = array('message' => $_message, 'type' => $_type);
            } else {
                $msg = $validate['msg'];
                $data = array('message' => $msg, 'type' => "error");
            }
        } else {
            $data = array('message' => "Selected Institute alredy added in this period", 'type' => "error");
        }


        echo json_encode($data);
    }

    function actionCreateDistributionMore() {
        $id = $_POST['id'];
        $card = $_POST['card'];
        $instituteId = $_POST['institute'];
        $sectionId = $_POST['section'];
        $employId = $_POST['id'];
        $notReturn = $_POST['nr'];
        $remove = $_POST['rm'];
        $returnDate = $_POST['returnDate'];
        $year = $_POST['year'];
        $month = $_POST['month'];
        $tYear = $_POST['toYear'];
        $tMonth = $_POST['toMonth'];
        $bgCard = $_POST['bgCard'];
        $isEdit = $_POST['edit'];
        $listId = $_POST['listId'];
        $tldCard = new TldCard();
        $isNotValid = $tldCard->validateMonthYear($instituteId, $sectionId, $month, $year);
        if ($isEdit) {
            $isNotValid = false;
        }
        if (!$isNotValid) {
            $tldCard->setValidIssueTldCardArray();
            $validate = $tldCard->validateIssueCardEdit($id, $card, $remove);

            if ($validate['valid']) {
                    $i = $tldCard->createDistributionEdit($id, $instituteId, $sectionId, $employId, $card, $notReturn, $returnDate, $year, $month, $remove, $tMonth, $tYear, $bgCard, $listId);
                
                if (is_bool($i)) {
                    if ($i) {
                        $_message = "Successsfuly Saved new Distribution ! ";
                        $_type = "success";
                    } else {
                        $_message = "Unsuccesssfuly Saved ! ";
                        $_type = "error";
                    }

                } else {
                    if ($i == -1) {
                        $_message = "No data was changed !";
                        $_type = "info";
                    } else if ($i == 1) {
                        $_message = "Successsfuly updated Section information ! ";
                        $_type = "success";
                    } else if ($i == 0) {
                        $_message = " Unsuccesssfuly update ! ";
                        $_type = "error";
                    } else {
                        $_message = $i . " is Not a valid TLD Card ";
                        $_type = "error";
                    }
                }
                $data = array('message' => $_message, 'type' => $_type);
            } else {
                $msg = $validate['msg'];
                $data = array('message' => $msg, 'type' => "error");
            }
        } else {
            $data = array('message' => "Selected Institute alredy added in this period", 'type' => "error");
        }


        echo json_encode($data);
    }

    function actionUpdateDistribution() {
        $id = $_POST['id'];
        $card = $_POST['card'];
        $notReturn = $_POST['nr'];
        $returnDate = $_POST['returnDate'];
        $fromDate = $_POST['fromDate'];
        $toDate = $_POST['toDate'];
        $tldCard = new TldCard();
        $i = $tldCard->updateDistribution($id, $card, $notReturn, $returnDate, $fromDate, $toDate);
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly Saved new Distribution ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly Saved ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Collection information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            } else {
                $_message = $i . " is Not a valid TLD Card ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);


        echo json_encode($data);

    }

    function actionUpdateDistributionCard() {
        $id = $_POST['id'];
        $card = $_POST['cardId'];
        $returnDate = $_POST['returnDate'];
        $fromDate = $_POST['fromDate'];
        $toDate = $_POST['toDate'];
        $tldCard = new TldCard();
        $i = $tldCard->updateDistributionCard($id, $card, $returnDate, $fromDate, $toDate);
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly Saved new Distribution ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly Saved ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Section information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            } else {
                $_message = $i . " is Not a valid TLD Card ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);


        echo json_encode($data);

    }


    function actionUpdateDistributionWithDose() {
        $id = $_POST['id'];
        $card = $_POST['card'];
        $dose = $_POST['dose'];
        $fromDate = $_POST['fromDate'];
        $toDate = $_POST['toDate'];
        $skip = $_POST['skip'];
        $tldCard = new TldCard();
        $validate = $tldCard->validateDoseValue($dose);
        if ($validate['valid']) {
            $i = $tldCard->updateDistributionWithDose($id, $card, $dose, $skip, $fromDate, $toDate);
            if (is_bool($i)) {
                if ($i) {
                    $_message = "Successsfuly Saved Dose ! ";
                    $_type = "success";
                } else {
                    $_message = "Unsuccesssfuly Saved ! ";
                    $_type = "error";
                }

            } else {
                if ($i == -1) {
                    $_message = "No data was changed !";
                    $_type = "info";
                } else if ($i == 1) {
                    $_message = "Successsfuly updated Dose Value information ! ";
                    $_type = "success";
                } else if ($i == 0) {
                    $_message = " Unsuccesssfuly update ! ";
                    $_type = "error";
                } else {
                    $_message = $i . " is Not a valid Dose Value ";
                    $_type = "error";
                }
            }
        } else {
            $_message = $validate['msg'];
            $_type = "error";
        }


        $data = array('message' => $_message, 'type' => $_type);


        echo json_encode($data);

    }

    function actionUpdateDoseManually() {
        $id = $_POST['id'];
        $dose = $_POST['doseValue'];
        $card = $_POST['card'];
        $tldCard = new TldCard();
        $validate = $tldCard->validateDoseValue(array($dose));
        if ($validate['valid']) {
            $i = $tldCard->updateDistributionWithDoseManually($id, $dose, $card);
            if (is_bool($i)) {
                if ($i) {
                    $_message = "Successsfuly Saved Dose ! ";
                    $_type = "success";
                } else {
                    $_message = "Unsuccesssfuly Saved ! ";
                    $_type = "error";
                }

            } else {
                if ($i == -1) {
                    $_message = "No data was changed !";
                    $_type = "info";
                } else if ($i == 1) {
                    $_message = "Successsfuly updated Dose Value information ! ";
                    $_type = "success";
                } else if ($i == 0) {
                    $_message = " Unsuccesssfuly update ! ";
                    $_type = "error";
                } else {
                    $_message = $i . " is Not a valid Dose Value ";
                    $_type = "error";
                }
            }
        } else {
            $_message = $validate['msg'];
            $_type = "error";
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);

    }


}
